/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package libraries;

import java.io.File;
import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 

public class XMLReader{
    static PathLibrary PL = new PathLibrary();
    static ArrayList<String> AttributeList = new ArrayList<String>();
    
    
    public void getData (String xml, String Tag, ArrayList<String> subtags){
        this.AttributeList.clear();  // Initialize variable.
        try {            
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse (new File(PL.getdcm4cheXMLPath()+"\\"+xml));
            //System.out.println("Path: "+PL.getdcm4cheXMLPath()+"\\"+xml);
            
            // normalize text representation
            doc.getDocumentElement ().normalize ();
            System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());


            NodeList listOfNodes = doc.getElementsByTagName(Tag);
            int totalNodes = listOfNodes.getLength();
            System.out.println("Total no of "+Tag+" : " + totalNodes);
            for(int i=0; i<listOfNodes.getLength() ; i++){

                Node currentNode = listOfNodes.item(i);
                if(currentNode.getNodeType() == Node.ELEMENT_NODE){
                    Element currentElement = (Element)currentNode;
                    
                    for(int j=0;j<subtags.size();j++){
                        String currentAttribute = currentElement.getAttribute(subtags.get(j));
                        this.AttributeList.add(currentAttribute);
                        //System.out.println("Attribute "+subtags.get(j)+": "+currentAttribute);
                        
                        /*NodeList firstTagList = currentElement.getElementsByTagName(subtags.get(j));                        
                        Element firstTagElement = (Element)firstTagList.item(0);

                        NodeList textFNList = firstTagElement.getChildNodes();
                        System.out.println("First Tag: " + 
                               ((Node)textFNList.item(0)).getNodeValue().trim());*/
                    }

                }//end of if clause


            }//end of for loop with s var
            //System.out.println(Tag+" Attribute List: "+AttributeList);     
            
        }catch (SAXParseException err) {
        System.out.println ("** Parsing error" + ", line " 
             + err.getLineNumber () + ", uri " + err.getSystemId ());
        System.out.println(" " + err.getMessage ());

        }catch (SAXException e) {
        Exception x = e.getException ();
        ((x == null) ? e : x).printStackTrace ();

        }catch (Throwable t) {
        t.printStackTrace ();
        }       
    }
    
    public String[] getStringList(){
        String[] AttributeListString = this.AttributeList.toArray(new String[AttributeList.size()]);
        return AttributeListString;
    }
    
    public ArrayList<String> getArrayList(){
        return this.AttributeList;
    }
}